package com.edu.web.blog;

import com.edu.entity.blog.Article;
import com.edu.entity.chat.Page;
import com.edu.service.blog.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: LuckyBear
 * @date: 2018-10-31 8:55
 * @description:
 */

@Controller
public class listBolgByCommentController {
    private static final int PAGE_COUNT = 4;

    @Autowired
    public ArticleService articleService;

    @RequestMapping("/listBlogByComment")
    public String listBlogByComment(HttpServletRequest request, Model model) {
        int currentPage = 1;
        String page = request.getParameter("page");
        if(page != null){
            currentPage = Integer.valueOf(page);
        }

        List<Article> articles = articleService.listBlogByComment(new Page((currentPage-1) * PAGE_COUNT,PAGE_COUNT));

        int totalPage = articleService.getTotal(PAGE_COUNT);
        model.addAttribute("articles",articles);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage","?page="+(currentPage+1));
        return "blog/blogIndex";
    }
}
